// pages/me/me.js
const get = require('../../utils/common.js')
const app = getApp();
Page({
  data: {
    userInfo: null,
    login_img: '',
  },
  onLoad: function (options) {
    let that = this
    wx.getStorage({
      key: 'urlId',
      success: function (res) {
        that.setData({ urlId: res.data })
      }
    })
    wx.getStorage({
      key: 'urlOpenid',
      success: function (res) {
        that.setData({ urlOpenid: res.data })
      }
    })
    that.getUserSessionKey()
    let openid = wx.getStorageSync('openid')
    that.setData({ openid })
    let sid = options.sid || that.data.urlId
    let upopenid = options.openid || that.data.urlOpenid
    that.setData({
      sid: sid,
      upopenid: upopenid
    })
    console.log(that.data.openid)
    //that.isLogin()
    if (options.sid) {
      that.formShare()
    } else {
      that.fromMoRen()
    }
  },
  isLogin() {
    var that = this
    var openid = that.data.openid
    console.log(openid)
    get.getData('/useractiveprofession/isgrant', { openid: openid }).then(res => {
      console.log(res)
      console.log(res.data.data)
      that.setData({
        isLogin: res.data.data
      })
    }).catch((e) => {
      var tip = e.data.msg
      wx.showToast({
        title: tip,
        duration: 500
      });
    })
  },
  onShow: function (options) {
    setTimeout(() => {
      this.checkuserGet()
      console.log('延迟')
    }, 500)
  },
  //默认的
  fromMoRen(res) {
    let that = this
    get.getData('/goods/goodsdetail').then(res => {
      console.log("登录页展示图", res.data.data)
      that.setData({
        login_img: res.data.data.title_img,
        goods_name: res.data.data.goods_name,
        moRenId: res.data.data.id,
        sid: res.data.data.id,
        link_type: res.data.data.link_type
      })
      console.log("登录页", that.data.link_type, that.data.sid, that.data.moRenId)
    }).catch((e) => {
      console.log(e, 'catch')
    })
  },
  //分享的
  formShare(res) {
    let that = this
    var sid = that.data.sid
    let upopenid = that.data.upopenid
    console.log(that.data.upopenid)
    get.getData('/useractiveprofession/share', { sid: sid, upopenid: upopenid }).then(res => {
      console.log(res)
      that.setData({
        login_img: res.data.data.title_img,
        goods_name: res.data.data.goods_name,
        moRenId: res.data.data.id,
        sid: res.data.data.id,
        link_type: res.data.data.link_type
      })
    }).catch((e) => {
      console.log(e, 'catch')
    })
  },

  //获取session_key和openid
  getUserSessionKey() {
    let that = this;
    wx.login({
      success(res) {
        console.log("wx.login", res);
        var code = res.code;
        get.getData('/useractiveprofession/getuserinfo', { code: code }).then(res => {
          console.log(res)
          var openid = res.data.data.openid
          console.log('openid', openid)
          var session_key = res.data.data.session_key
          app.globalData.openid = openid
          app.globalData.session_key = session_key
          console.log(openid, session_key)
          that.setData({
            openid: openid,
            session_key: session_key
          })
          console.log(that.data.openid, that.data.session_key)
          wx.setStorage({
            key: 'openid',
            data: openid,
          });
          wx.setStorage({
            key: 'session_key',
            data: session_key,
          });
          that.checkuserGet()
        }).catch((e) => {
          console.log(e, 'catch')
        })
      }
    })

  },
  //onLoginUser
  onLoginUser(telNum) {
    var that = this
    var user = that.data.userInfo
    var openid = that.data.openid
    var upopenid = that.data.upopenid || ''
    var session_key = that.data.session_key
    get.getData('/useractiveprofession/userinfo', { openid, session_key, upopenid, tel: telNum }).then(res => {
      wx.showToast({
        title: '登录成功',
        duration: 500
      });
      if (that.data.sid && that.data.link_type == 2) {
        console.log(that.data.sid)
        wx.navigateTo({
          url: "/pages/content/content?sid=" + that.data.sid + "&telNum=" + telNum + "&upopenid=" + upopenid
        })
      } else if (that.data.sid && that.data.link_type == 1) {
        wx.navigateTo({
          url: "/pages/list/list_detail/list_detail?sid=" + that.data.sid + "&telNum=" + telNum + "&upopenid=" + upopenid
        })
      } else {
        wx.navigateTo({
          url: "/pages/detail/detail?moRenId=" + that.data.moRenId + "&telNum=" + telNum + "&upopenid=" + upopenid
        })
      }
    }).catch(err => {
      wx.showToast({
        title: '网络异常！err:authlogin',
        duration: 2000
      })
    })


  },
  //获取手机号码
  getPhoneNumber(e) {
    console.log(235)
    var that = this
    var session_key = that.data.session_key
    var openid = that.data.openid
    let encryptedData = e.detail.encryptedData
    let iv = e.detail.iv
    get.getData('/useractiveprofession/gettelnumber', { session_key: session_key, openid: openid, encryptedData: encryptedData, iv: iv }).then(res => {
      if (res.data.code == 10008) {
        let telJson = res.data.data
        telJson = JSON.parse(res.data.data)
        let telNum = telJson.phoneNumber
        that.setData({
          telNum: telNum
        })
        wx.setStorage({
          key: 'telNum',
          data: telNum
        });
        that.onLoginUser(telNum) //上传upopenid tel 之后判断跳转类型为商品页 还是文章页
      } else {
        wx.showToast({
          title: '请先授权手机号',
          image: '/pages/images/fail.png',
          cancelColor: '#f00',
          confirmColor: '#0DD164',
          duration: 1000
        });
        // that.hideModel()
      }
    }).catch(err => {

    })

  },
  //查看分享数据
  toShare(res) {
    wx.redirectTo({
      url: '/pages/share/share',
    })
  },
  //判断是否分享可见
  // checkuser(e) {
  //   console.log(e)
  //   let that = this
  //   var openid = that.data.openid
  //   console.log(openid)
  //   get.getData('/useractiveprofession/checkuser', { openid: e }).then(res => {
  //     console.log(res)
  //     console.log(res.data.data)
  //     that.setData({ simsonStaff: res.data.data })
  //   }).catch(err => {

  //   })
  // },
  checkuserGet() {
    let that = this
    var openid = that.data.openid
    var moRenId = that.data.moRenId
    console.log(openid, moRenId)
    get.getData('/useractiveprofession/check_auth', { openid: openid || '' }).then(res => {
      console.log('res.data.data', res.data.data)
      that.setData({
        simsonData: res.data.data,
        allowauth: res.data.allowauth,
        tel_state: res.data.tel_state,
      })
      console.log('触发', openid, that.data.simsonData, that.data.allowauth, that.data.tel_state)
    }).catch(err => {
      console.log('err')
    })
  },
  getPhoneNumberauth(e) {
    console.log(e)
    var moRenId = this.data.moRenId
    var link_type = this.data.link_type
    var upopenid = this.data.upopenid
    console.log(moRenId, link_type)
    if (link_type == 1) {
      wx.redirectTo({
        url: '/pages/detail/detail?moRenId=' + moRenId + "&upopenid=" + upopenid,
      })
    } else if (link_type == 2) {
      wx.redirectTo({
        url: '/pages/content/content?moRenId=' + moRenId,
      })
    }

  },
})